<?php

namespace App\Kuafu\WaiMai\Command;

use App\Kuafu\Config\Model\ConfigModel;
use App\Kuafu\WaiMai\Channel\ElmWaiMai;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;

class WaiMaiCheckToken extends Command
{
    protected $signature = 'waimai:check-token';

    protected $description = '外卖-检查饿了么token有效性';

    public function handle()
    {
        $shopId = (int)ConfigModel::query()->where('key', 'elmApiShop')->value('value');
        if (!$shopId) {
            return;
        }

        $elm = ElmWaiMai::getIns(WaiMaiDBEnum::SOURCE_TYPE_ELM, $shopId);

        $data = $elm->getProductPage(1, 1);
        dump($data);

        if ($data['msg'] == 'token认证失败,请重新申请token') {
            $elm->reloadToken();
            $reData = ($elm->getProductPage(1, 1));

            $content = '';
            if ($reData['status']) {
                $content .= "已重新获取token\n";
            } else {
                $content .= "重新获取token失败\n" . ($reData['msg'] ?? '');
            }

            SendFs::newRobot()->setTitleContent('饿了么token过期', $content, '9efgbcdb')->send();
        }
    }
}
